#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
测试管理员API接口
"""

import requests
import json

# 配置
BASE_URL = 'http://127.0.0.1:8080'
ADMIN_ID = 'admin001'  # 请替换为实际的管理员ID

def print_section(title):
    """打印分隔线"""
    print("\n" + "="*70)
    print(" " * 25 + title)
    print("="*70 + "\n")

def print_result(name, response):
    """打印测试结果"""
    print("[测试] %s" % name)
    print("状态码: %d" % response.status_code)
    try:
        data = response.json()
        print("返回数据:")
        print(json.dumps(data, indent=2, ensure_ascii=False))
    except:
        print("响应内容: %s" % response.text)
    print("-" * 70)

def test_dashboard():
    """测试仪表板"""
    print_section("1. 测试仪表板")
    
    url = "%s/admin/dashboard" % BASE_URL
    params = {'admin_id': ADMIN_ID}
    
    response = requests.get(url, params=params)
    print_result("获取仪表板数据", response)

def test_users():
    """测试用户管理"""
    print_section("2. 测试用户管理")
    
    # 2.1 获取用户列表
    url = "%s/admin/users" % BASE_URL
    params = {
        'admin_id': ADMIN_ID,
        'page': 1,
        'size': 10
    }
    
    response = requests.get(url, params=params)
    print_result("获取用户列表", response)
    
    # 2.2 获取用户详情（使用管理员自己的ID）
    url = "%s/admin/user/detail" % BASE_URL
    params = {
        'admin_id': ADMIN_ID,
        'user_id': ADMIN_ID
    }
    
    response = requests.get(url, params=params)
    print_result("获取用户详情", response)

def test_stores():
    """测试商家管理"""
    print_section("3. 测试商家管理")
    
    # 3.1 获取商家列表
    url = "%s/admin/stores" % BASE_URL
    params = {
        'admin_id': ADMIN_ID,
        'page': 1,
        'size': 10
    }
    
    response = requests.get(url, params=params)
    print_result("获取商家列表", response)
    
    # 3.2 获取待审核商家
    url = "%s/admin/stores/pending" % BASE_URL
    params = {'admin_id': ADMIN_ID}
    
    response = requests.get(url, params=params)
    print_result("获取待审核商家", response)

def test_takeouts():
    """测试外卖管理"""
    print_section("4. 测试外卖管理")
    
    # 4.1 获取外卖列表
    url = "%s/admin/takeouts" % BASE_URL
    params = {
        'admin_id': ADMIN_ID,
        'page': 1,
        'size': 10
    }
    
    response = requests.get(url, params=params)
    print_result("获取外卖列表", response)
    
    # 4.2 获取待审核外卖
    url = "%s/admin/takeouts/pending" % BASE_URL
    params = {'admin_id': ADMIN_ID}
    
    response = requests.get(url, params=params)
    print_result("获取待审核外卖", response)

def test_orders():
    """测试订单管理"""
    print_section("5. 测试订单管理")
    
    url = "%s/admin/orders" % BASE_URL
    params = {
        'admin_id': ADMIN_ID,
        'page': 1,
        'size': 10
    }
    
    response = requests.get(url, params=params)
    print_result("获取订单列表", response)

def test_statistics():
    """测试统计分析"""
    print_section("6. 测试统计分析")
    
    url = "%s/admin/statistics" % BASE_URL
    params = {
        'admin_id': ADMIN_ID,
        'days': 7
    }
    
    response = requests.get(url, params=params)
    print_result("获取统计数据", response)

def main():
    """主函数"""
    print("\n")
    print("*" * 70)
    print(" " * 20 + "管理员API接口测试")
    print("*" * 70)
    print("\n配置信息:")
    print("  服务器地址: %s" % BASE_URL)
    print("  管理员ID: %s" % ADMIN_ID)
    print("\n提示:")
    print("  1. 请确保后端服务器正在运行")
    print("  2. 请确保ADMIN_ID是正确的管理员用户ID")
    print("  3. 如果不知道管理员ID，请先运行: python 查看数据库.py")
    print("\n")
    
    input("按回车键开始测试...")
    
    try:
        # 执行测试
        test_dashboard()
        test_users()
        test_stores()
        test_takeouts()
        test_orders()
        test_statistics()
        
        print_section("测试完成")
        print("所有测试已执行完毕！")
        print("\n如果看到错误，请检查：")
        print("1. 后端服务器是否运行在 %s" % BASE_URL)
        print("2. 管理员ID '%s' 是否正确" % ADMIN_ID)
        print("3. 数据库中是否有相应的数据\n")
        
    except requests.exceptions.ConnectionError:
        print("\n[错误] 无法连接到服务器！")
        print("请确保后端服务器正在运行: python manage.py runserver 0.0.0.0:8080\n")
    except Exception as e:
        print("\n[错误] 测试过程中出现异常: %s\n" % str(e))
        import traceback
        traceback.print_exc()

if __name__ == '__main__':
    main()

